package org.tio.im.client;

//
//  https://github.com/WelkinXie/AESCipher-Java
//

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
/**
 * AES加密类--通用JAVA iOS
*    
* 项目名称：social   
* 类名称：AESCipher   
* 类描述：   
* 创建人：liuyong  
* 创建时间：2017年8月6日 下午3:46:13   
* @version
 */
public class AESCipher {
	private static Logger log = LoggerFactory.getLogger(AESCipher.class);
	private static final String IV_STRING = "1cl54f19a71f1e0j";
	private static final String charset = "UTF-8";
	 
	public static String aesEncryptString(String content, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
		byte[] contentBytes = content.getBytes(charset);
		byte[] keyBytes = key.getBytes(charset);
		byte[] encryptedBytes = aesEncryptBytes(contentBytes, keyBytes);
		//return org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(encryptedBytes);
		Encoder encoder = Base64.getEncoder();
	    return encoder.encodeToString(encryptedBytes);
	}

	public static String aesDecryptString(String content, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
		Decoder decoder = Base64.getDecoder();
	   // byte[] encryptedBytes = org.apache.commons.codec.binary.Base64.decodeBase64(content);
	    byte[] encryptedBytes = decoder.decode(content);
	    byte[] keyBytes = key.getBytes(charset);
		byte[] decryptedBytes = aesDecryptBytes(encryptedBytes, keyBytes);
	    return new String(decryptedBytes, charset);		
	}
	
	public static byte[] aesEncryptBytes(byte[] contentBytes, byte[] keyBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
	    return cipherOperation(contentBytes, keyBytes, Cipher.ENCRYPT_MODE);
	}
	
	public static byte[] aesDecryptBytes(byte[] contentBytes, byte[] keyBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
	    return cipherOperation(contentBytes, keyBytes, Cipher.DECRYPT_MODE);
	}
	
	private static byte[] cipherOperation(byte[] contentBytes, byte[] keyBytes, int mode) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
		SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
		
	    byte[] initParam = IV_STRING.getBytes(charset);
	    IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);

	    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
	    cipher.init(mode, secretKey, ivParameterSpec);
	    
 	 	return cipher.doFinal(contentBytes);
	}
	
/*	public static void main(String[] args) throws Exception {
		//"16BytesLengthKey"
		// 密钥16位
		   String secretKey = "154f19a71f1e0jea";
		String string = AESCipher.aesEncryptString("{\"a\":12345}", secretKey);
		System.out.println(string);
		System.out.println(AESCipher.aesDecryptString(string, secretKey));
		
		string = "HYeNbSqZ+piMUeQZsBzuEA==";
		System.out.println(AESCipher.aesDecryptString(string, secretKey));
		
		String k = "{\"login\":\"0\",\"refreshToken\":\"d2577\",\"t1\":\"dba066e4\",\"t2\":\"ca5\",\"token\":\"tp3901a786c5c5d450499bb3af1a1c8dfec\"}";
	System.out.println(AESCipher.aesCommEncrypt(k));	;
	
	//System.out.println(AESCipher.aesCommDecrypt("IvRFaUN3gXxIxlrYfWjMyEQbEK1fA1xOzDsE2D5og4YC0qVjiHt8rJElgPhdCES8mMUZ+gt2+6oLjbAagZElo1L2ohhGC5894Q+rDVA0cHnXpvb2fCq8S0ma+2aJnxRo1HiYRvUaHp13i4TD2H0i9w=="));	;
	}*/
	/**
	 * 解密第一次打开用固定的key--使用固定的KEY
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String aesCommDecrypt(String data) {
		try {
			return  AESCipher.aesDecryptString(data, secretKey);
		} catch (Exception e) {
			log.error("解密异常",e);
		}
		return null;
	}
	public static final String secretKey = "154f19a71f1e0jea";
	/**
	 * 解密第一次打开用固定的key--使用固定的KEY
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String aesCommEncrypt(String data) {
		try {
			return  AESCipher.aesEncryptString(data, secretKey);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	/**
	 * 加密数据
	 * @param data 待加密的数据
	 * @param secretKey 加密串
	 * @return
	 * @throws Exception
	 */
	public static String aesEncrypt(String data,String secretKey) throws Exception {
		return  AESCipher.aesEncryptString(data, secretKey);
	}
	/**
	 * 解密
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String aesDecrypt(String data,String secretKey) throws Exception {
		return  AESCipher.aesDecryptString(data, secretKey);
	}

	
	/**
	 * 解密第一次打开用固定的key--使用固定的KEY--微信专用
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String aesWxCommDecrypt(String data) throws Exception {
		return  AESCipher.aesDecryptString(data, wxSecretKey);
	}
	/**
	 * 解密第一次打开用固定的key--使用固定的KEY--微信专用
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String aesWxCommEncrypt(String data) throws Exception {

		return  AESCipher.aesEncryptString(data, wxSecretKey);
	}
	//public static final String wxSecretKey = "754f18a71f1e0qev";
	public static final String wxSecretKey = "854f18a71f1e0qev";
}
